import 'package:flutter/material.dart';
import '../widgets/toast_notifications.dart';
import '../widgets/simple_loading_overlay.dart';

/// 通知工具类
/// 统一管理Toast提示和加载组件
class NotificationUtils {
  /// 显示成功提示
  static void showSuccess(
    BuildContext context, {
    required String message,
    String? title,
    Duration? duration,
  }) {
    ToastNotificationHelper.showSuccessToast(
      context,
      message: message,
      title: title,
      duration: duration ?? const Duration(seconds: 3),
    );
  }

  /// 显示错误提示
  static void showError(
    BuildContext context, {
    required String message,
    String? title,
    Duration? duration,
  }) {
    ToastNotificationHelper.showErrorToast(
      context,
      message: message,
      title: title,
      duration: duration ?? const Duration(seconds: 3),
    );
  }

  /// 显示警告提示
  static void showWarning(
    BuildContext context, {
    required String message,
    String? title,
    Duration? duration,
  }) {
    ToastNotificationHelper.showWarningToast(
      context,
      message: message,
      title: title,
      duration: duration ?? const Duration(seconds: 3),
    );
  }

  /// 显示信息提示
  static void showInfo(
    BuildContext context, {
    required String message,
    String? title,
    Duration? duration,
  }) {
    ToastNotificationHelper.showInfoToast(
      context,
      message: message,
      title: title,
      duration: duration ?? const Duration(seconds: 3),
    );
  }

  /// 显示加载蒙层
  static void showLoading(BuildContext context, {String? message}) {
    SimpleLoadingOverlay.show(context, message: message);
  }

  /// 隐藏加载蒙层
  static void hideLoading() {
    SimpleLoadingOverlay.hide();
  }

  /// 检查是否正在显示加载蒙层
  static bool isLoadingVisible() {
    return SimpleLoadingOverlay.isShowing();
  }

  /// 清除所有Toast
  static void clearAllToasts(BuildContext context) {
    ToastNotificationHelper.clearAllToasts(context);
  }

  /// 显示带加载的异步操作
  static Future<T?> executeWithLoading<T>(
    BuildContext context,
    Future<T> Function() operation, {
    String? loadingMessage,
    String? successMessage,
    String? errorMessage,
    bool showSuccessToast = false,
    bool showErrorToast = true,
  }) async {
    try {
      // 显示加载
      NotificationUtils.showLoading(context, message: loadingMessage);

      // 执行操作
      final result = await operation();

      // 隐藏加载
      hideLoading();

      // 显示成功提示
      if (showSuccessToast && successMessage != null) {
        showSuccess(context, message: successMessage);
      }

      return result;
    } catch (e) {
      // 隐藏加载
      hideLoading();

      // 显示错误提示
      if (showErrorToast) {
        showError(context, message: errorMessage ?? '操作失败: ${e.toString()}');
      }

      return null;
    }
  }
}
